package interfaces.menu.joinGame.gamesTable;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import interfaces.language.LanguageButton;
import interfaces.menu.joinGame.gamesTable.GamesTable.Column;

public class GamesTableButton extends LanguageButton {
	
	public enum OrderMode { Ascending, Descending, NotOrdered }
	
	private GamesTable gamesTable;
	private OrderMode mode = OrderMode.NotOrdered;
	private Column column;
	
	public GamesTableButton(Lang text, GamesTable gamesTable, Column column, ImageFont font) {
		super(text, font);
		this.gamesTable = gamesTable;
		this.column = column;
		
		addButtonPressedListener(getListener());
	}
	
	private IButtonPressedListener getListener() {
		return new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				switch(mode) {
					case NotOrdered: changeOrderMode(OrderMode.Ascending); break;
					case Ascending: changeOrderMode(OrderMode.Descending); break;
					case Descending: changeOrderMode(OrderMode.Ascending); break;
				}
				gamesTable.order(column, mode == OrderMode.Ascending);
			}
		};
	}
	
	public void changeOrderMode(OrderMode mode) {
		this.mode = mode;
		String postFix = "";
		switch(mode) {
			case NotOrdered: postFix = ""; break;
			case Ascending: postFix = " (A)"; break;
			case Descending: postFix = " (D)"; break;
		}
		setText(LanguageLoader.get(text) + postFix);
	}
	
	public Column getColumn() { return column; }
	
	@Override
	public void changeLanguage() { changeOrderMode(mode); }
}